package com.seuic.hnapps.util;

import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.solab.enc.HexT;
import com.seuic.hnapps.util.RSAUtils;
import com.solab.iso8583.IsoMessage;
import com.solab.iso8583.IsoType;


/**
 *
 * @author Administrator
 * 申请终端主密钥
 */

public class RTKInterface implements YanTaInterface{
	private static final Logger log = LoggerFactory.getLogger(RTKInterface.class);
	// 私钥，终端临时保存
	RSAPrivateKey privateKey = null;

	public IsoMessage packRequest(String transn) {

		IsoMessage msg = new IsoMessage();

		msg.setType(0x0820);
		msg.setHeadValue(1, String.valueOf(Constants.ATMP_HEADER_LENGTH), IsoType.BCD, 2);
		msg.setHeadValue(2, Constants.YANTAI_PACKVS, IsoType.ALPHA, 2);
		msg.setHeadValue(3, Constants.YANTAI_TERMVS, IsoType.ALPHA, 8);
		msg.setHeadValue(4, Constants.YANTAI_ENCFLG1, IsoType.ALPHA, 1); // 加密标志。管理类交易不需加密
		msg.setHeadValue(5, Constants.YANTAI_BANKID, IsoType.ALPHA, 8);

		//根据设备终端号自动填充，测试用10400501
		msg.setHeadValue(6, Constants.YANTAI_TERMNO, IsoType.ALPHA, 8);

		msg.setHeadValue(7, "0000", IsoType.ALPHA, 4); // 8583域部分长度，组包时动态填充


		msg.setValue(7, DateUtil.getCurrentTime(DateUtil.M_TIME_PATTERN), IsoType.ALPHA, 10);
		msg.setValue(8, Constants.YANTAI_CHANLENO, IsoType.ALPHA, 2);
		msg.setValue(11, transn, IsoType.ALPHA, 6);

		//根据设备终端号自动填充，测试用10400501
		msg.setValue(41, Constants.YANTAI_TERMNO, IsoType.ALPHA, 8);


		Map<String, Object> keyMap = null;
		RSAPublicKey publicKey  = null;

		try {
			keyMap = RSAUtils.genKeyPair();
			publicKey = (RSAPublicKey) keyMap.get(RSAUtils.PUBLIC_KEY);
			privateKey = (RSAPrivateKey) keyMap.get(RSAUtils.PRIVATE_KEY);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String skey = HexT.byteArr2HexStr(publicKey.getEncoded());
		String tag = "UK";
		String sblen = ServiceCommon.appendZero(skey.length(), 3);
		String s48 = tag + sblen + skey;

		msg.setValue(48, s48, IsoType.LLLVAR, 512);
		if(Constants.isSM){
			msg.setValue(53, Constants.DEMO_F53, IsoType.ALPHA, 16);
		}

		msg.setValue(70, "001", IsoType.ALPHA, 3);


		return msg;
	}


	public void unpackReSpone(IsoMessage response) {
		com.orhanobut.logger.Logger.e(response+"responce");
		if(response.getField(61)==null){
			log.error("错误：后台没有返回密钥信息!");
		}else{
			String f61 = response.getStringValue(61);
			String tag = f61.substring(0, 2);
			String slen = f61.substring(2, 5);
			int len = Integer.parseInt(slen);
			String sdata = f61.substring(5, 5+len);
			log.debug("返回数据：\r\n" + tag+"--"+slen+"->"+sdata);
			byte[] encodedData = HexT.hexstr2ByteArr(sdata);
			byte[] decdata2 = null;
			try {
				decdata2 = RSAUtils.decryptByPrivateKey(encodedData, privateKey.getEncoded());
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			String atmtmk = new String(decdata2);
			log.debug("返回atmtmk：\r\n" + atmtmk);
//			com.orhanobut.logger.Logger.e(atmtmk);
			atmtmk = atmtmk.substring(atmtmk.length()-32, atmtmk.length());
			com.orhanobut.logger.Logger.e(atmtmk);
			/**
			 * 终端主密钥灌入密码键盘处理
			 */

			// 本demo后续测试需要，手工保存起来
			Constants.YANTAI_TERMTMK = atmtmk;
		}

	}

}